package box
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	public class HelloBox extends Sprite
	{
		public var world:b2World;
		
		public static const RADIO:Number = 30;
		public static const GRAVITY:Number = 9.8;
		
		public static const timeStep:Number = 1/30;
		public static const velocityIteration:int = 10;
		public static const positionIteration:int = 10;
		
		public function HelloBox()
		{
			setupWorld();
			setupGround();
			setupOthers();
			addEventListener(Event.ENTER_FRAME,loop);
		}
		
		private function getSprite():Sprite{
			var sprite:Sprite = new Sprite();
				sprite.graphics.beginFill(0xabcef);
				sprite.graphics.drawRect(0,0,640,480);
				sprite.graphics.endFill();
			return sprite;
		}
		
		private function setupWorld():void{
			var gravity:b2Vec2 = new b2Vec2();
				gravity.Set(0,GRAVITY);
			
			var doSleep:Boolean = true;
			world = new b2World(gravity,doSleep);
		}
		
		private function setupGround():void{
			var sprite:Sprite = getSprite();
				sprite.height = 100;
				sprite.width = 640;
				sprite.y = 480 - sprite.height;
			addChild(sprite);
			
			var bodyDef:b2BodyDef = new b2BodyDef();
				bodyDef.userData = sprite;
				bodyDef.position = new b2Vec2(sprite.x / RADIO,sprite.y / RADIO);
			
			var shape:b2PolygonShape = new b2PolygonShape();
				shape.SetAsBox(sprite.width/RADIO/2,sprite.height/RADIO/2);
			
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
				fixtureDef.friction = 0.6;
				fixtureDef.restitution = 0.1;
				fixtureDef.density = 0;
				fixtureDef.shape = shape;
				
			var body:b2Body = world.CreateBody(bodyDef);
				body.CreateFixture(fixtureDef);
				b2Body.b2_dynamicBody
			trace("world ok",world.GetBodyCount());
		}
		
		private function setupOthers():void{
			var sprite:Sprite = getSprite();
				sprite.width = 100;
				sprite.height = 100;
				addChild(sprite);
			
			var bodyDef:b2BodyDef = new b2BodyDef();
				bodyDef.position = new b2Vec2(sprite.x / RADIO,sprite.y / RADIO);
				bodyDef.type = b2Body.b2_dynamicBody;
				bodyDef.userData = sprite;
				
			var shape:b2PolygonShape = new b2PolygonShape();
				shape.SetAsBox(sprite.width/2/RADIO,sprite.height/2/RADIO);
			
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
				fixtureDef.density = 1;
				fixtureDef.friction = 0.4;
				fixtureDef.restitution = 0.3;
				fixtureDef.shape = shape;
			
			var body:b2Body = world.CreateBody(bodyDef);
				body.CreateFixture(fixtureDef);
		}
		
		private function loop(e:Event):void{
			world.Step(timeStep,velocityIteration,positionIteration);
			
			for(var bb:b2Body = world.GetBodyList();bb;bb = bb.GetNext()){
				var do2d:DisplayObject = bb.GetUserData();
					if(do2d){
						var vec2:b2Vec2 = bb.GetPosition();
						
						var angle:Number = bb.GetAngle();
							do2d.x = vec2.x * RADIO;
							do2d.y = vec2.y * RADIO;
							do2d.rotation = angle * 180 / Math.PI;
					}
			}
		}
		
	}
}